﻿namespace Services.Implement;

/// <summary>
/// 角色服务
/// </summary>
public class FeatureBLL(ISqlSugarClient db) : BaseBLL<Feature>(db), IFeatureBLL
{
    private readonly ISqlSugarClient _db = db;

    /// <summary>
    /// 角色列表
    /// </summary>
    /// <returns></returns>
    public async Task<List<Feature>> GetAllAsync(Expression<Func<Feature, bool>>? whereExpression = null)
    {
        if (whereExpression == null)
        {
            whereExpression = x => x.Enable == 1;
        }
        else
        {
            whereExpression.And(x => x.Enable == 1);
        }
        return await GetListAsync(whereExpression);
    }
    /// <summary>
    /// 获取功能树形结构
    /// </summary>
    /// <param name="features"></param>
    /// <param name="selectedIds"></param>
    /// <returns></returns>
    public List<FeatureTree> GetFeatureTree(List<Feature> features, List<int>? selectedIds = null)
    {
        List<Feature> firstLevelItems = features.Where(f => f.Pid == 0).OrderBy(f => f.Order).ToList();
        List<FeatureTree> featureTrees = new List<FeatureTree>();
        foreach (Feature item in firstLevelItems)
        {
            FeatureTree featureTree = new FeatureTree()
            {
                Id = item.Id,
                Name = item.Name,
                Type = item.Type,
                RoutUrl = item.RoutUrl,
                Selected = selectedIds != null && selectedIds.Contains(item.Id),
                Children = GetChildren(item.Id, features, selectedIds)
            };
            featureTrees.Add(featureTree);
        }
        return featureTrees;
    }
    /// <summary>
    /// 获取功能树形结构
    /// </summary>
    /// <param name="features"></param>
    /// <param name="selectedIds"></param>
    /// <returns></returns>
    public List<FeatureTree> GetFeatureTreeMenu(List<Feature> features, List<int>? selectedIds = null)
    {
        List<Feature> firstLevelItems = features.Where(f => f.Pid == 0).OrderBy(f => f.Order).ToList();
        List<FeatureTree> featureTrees = new List<FeatureTree>();
        foreach (Feature item in firstLevelItems)
        {
            FeatureTree featureTree = new FeatureTree()
            {
                Id = item.Id,
                Name = item.Name,
                Type = item.Type,
                RoutUrl = item.RoutUrl,
                Selected = selectedIds != null && selectedIds.Contains(item.Id),
                Children = GetChildrenMenu(item.Id, features, selectedIds)
            };
            featureTrees.Add(featureTree);
        }
        return featureTrees;
    }

    /// <summary>
    /// 获取子菜单元素
    /// </summary>
    /// <param name="id"></param>
    /// <param name="features"></param>
    /// <param name="featureIds"></param>
    /// <returns></returns>
    public List<int> GetChildIds(int id, List<Feature> features, List<int>? featureIds = null)
    {
        List<Feature> children = features.Where(f => f.Pid == id).OrderBy(f => f.Order).ToList();
        if (!children.Any())
        {
            return new List<int>();
        }
        if (featureIds == null)
        {
            featureIds = new List<int>();
        }
        foreach (Feature child in children)
        {
            if (child.Type == 2)
            {
                featureIds.Add(child.Id);
            }
            if (child.Type != 3)
            {
                GetChildIds(child.Id, features, featureIds);
            }
        }
        return featureIds;
    }
    /// <summary>
    /// 获取子项目
    /// </summary>
    /// <param name="id"></param>
    /// <param name="selectedIds"></param>
    /// <param name="features"></param>
    /// <returns></returns>
    private List<FeatureTree> GetChildren(int id, List<Feature> features, List<int> selectedIds)
    {
        List<Feature> children = features.Where(f => f.Pid == id).OrderBy(f => f.Order).ToList();
        if (!children.Any())
        {
            return new List<FeatureTree>();
        }

        List<FeatureTree> trees = new List<FeatureTree>();
        foreach (Feature child in children)
        {

            FeatureTree featureTree = new FeatureTree()
            {
                Id = child.Id,
                Name = child.Name,
                Type = child.Type,
                RoutUrl = child.RoutUrl,
                Selected = selectedIds != null && selectedIds.Contains(child.Id),
                Children = GetChildren(child.Id, features, selectedIds)
            };
            trees.Add(featureTree);
        }
        return trees;
    }
    private List<FeatureTree> GetChildrenMenu(int id, List<Feature> features, List<int> selectedIds)
    {
        List<Feature> children = features.Where(f => f.Pid == id).OrderBy(f => f.Order).ToList();
        if (!children.Any())
        {
            return new List<FeatureTree>();
        }

        List<FeatureTree> trees = new List<FeatureTree>();
        foreach (Feature child in children)
        {
            if (child.Type <= 2)
            {
                FeatureTree featureTree = new FeatureTree()
                {
                    Id = child.Id,
                    Name = child.Name,
                    Type = child.Type,
                    RoutUrl = child.RoutUrl,
                    Selected = selectedIds != null && selectedIds.Contains(child.Id),
                    Children = GetChildrenMenu(child.Id, features, selectedIds)
                };
                trees.Add(featureTree);
            }
        }
        return trees;
    }

}
